package model.impl;

import java.sql.SQLException;
import java.util.List;

import model.bo.Dtable;
import model.dao.IDtableDAO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * Data access object (DAO) for domain model class Dtable.
 * 
 * @see model.bo.Dtable
 * @author MyEclipse Persistence Tools
 */

public class DtableDAO extends HibernateDaoSupport implements IDtableDAO {
	private static final Log log = LogFactory.getLog(DtableDAO.class);

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see model.impl.IDtableDAO#save(model.bo.Dtable)
	 */
	public void save(Dtable transientInstance) {
		log.debug("saving Dtable instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.impl.IDtableDAO#delete(model.bo.Dtable)
	 */
	public void delete(Dtable persistentInstance) {
		log.debug("deleting Dtable instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.impl.IDtableDAO#findById(java.lang.Integer)
	 */
	public Dtable findById(java.lang.Integer id) {
		log.debug("getting Dtable instance with id: " + id);
		try {
			Dtable instance = (Dtable) getHibernateTemplate().get(
					"model.bo.Dtable", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.impl.IDtableDAO#findByExample(model.bo.Dtable)
	 */
	public List findByExample(Dtable instance) {
		log.debug("finding Dtable instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.impl.IDtableDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Dtable instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Dtable as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.impl.IDtableDAO#findByTablename(java.lang.Object)
	 */
	public List findByTablename(Object tablename) {
		return findByProperty(TABLENAME, tablename);
	}

	/* (non-Javadoc)
	 * @see model.impl.IDtableDAO#findByTableurl(java.lang.Object)
	 */
	public List findByTableurl(Object tableurl) {
		return findByProperty(TABLEURL, tableurl);
	}

	/* (non-Javadoc)
	 * @see model.impl.IDtableDAO#findByTalbetype(java.lang.Object)
	 */
	public List findByTalbetype(Object talbetype) {
		return findByProperty(TALBETYPE, talbetype);
	}

	/* (non-Javadoc)
	 * @see model.impl.IDtableDAO#findByTalbetypename(java.lang.Object)
	 */
	public List findByTalbetypename(Object talbetypename) {
		return findByProperty(TALBETYPENAME, talbetypename);
	}

	/* (non-Javadoc)
	 * @see model.impl.IDtableDAO#findAll()
	 */
	public List findAll() {
		log.debug("finding all Dtable instances");
		try {
			String queryString = "from Dtable";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.impl.IDtableDAO#merge(model.bo.Dtable)
	 */
	public Dtable merge(Dtable detachedInstance) {
		log.debug("merging Dtable instance");
		try {
			Dtable result = (Dtable) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.impl.IDtableDAO#attachDirty(model.bo.Dtable)
	 */
	public void attachDirty(Dtable instance) {
		log.debug("attaching dirty Dtable instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see model.impl.IDtableDAO#attachClean(model.bo.Dtable)
	 */
	public void attachClean(Dtable instance) {
		log.debug("attaching clean Dtable instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static IDtableDAO getFromApplicationContext(ApplicationContext ctx) {
		return (IDtableDAO) ctx.getBean("DtableDAO");
	}
	
//	通过HQL返回查询结果总数
	public int getAllRowCount(String hql) {
		return getHibernateTemplate().find(hql).size();
	}

	//通过HQL查询,带分页.
	public List queryForPage(final String hql, final int offset,
			final int length) {
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				query.setFirstResult(offset);
				query.setMaxResults(length);
				List list = query.list();
				return list;
			}
		});
		return list;
	}

	//hib回调方法
	public int execusql(final String sql) {
		Session session = this.getSession();
		Transaction tx = session.beginTransaction();
		int result = -1;
		try {
			tx.begin();
			result = session.createSQLQuery(sql).executeUpdate();
			session.flush();
			tx.commit();
		} catch (DataAccessException e) {
			e.printStackTrace();
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			session.close();
		}
		return result;
	}
}